package com.ibeacon.controller.common;

import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 页面常用方法
 * @author yangh
 *
 */
public class PageBase {
	/**
	 * 设置cookie
	 * @param name
	 * @param value
	 */
	public void setCookie(String name,String value,HttpServletResponse response)throws Exception{
		Cookie cookie = new Cookie(name,URLEncoder.encode(value,"UTF-8"));
		response.addCookie(cookie);
	}
	/**
	 * 获取cookie的值
	 * @param CookieName
	 * @return cookie的值
	 */
	public String getCookie(String cookieName,HttpServletRequest request)throws Exception{
		Cookie[] cookies = request.getCookies();
		if(cookies!=null){
			for(Cookie cookie : cookies){
				if(cookie.getName().equals(cookieName))
				{
					return URLDecoder.decode(cookie.getValue(),"UTF-8");
				}
			}
		}
		return null;
	}
	/**
	 * 删除Cookie
	 * @param cookieName
	 * @param response
	 */
	public void DeleteCookie(String cookieName,HttpServletResponse response)throws Exception{
		Cookie cookie = new Cookie(cookieName,null);
		cookie.setMaxAge(0);
		response.addCookie(cookie);
	}
	/**
	 * 获取是星期几
	 * @param day
	 * @return
	 */
	public String getWeekName(int day){
		switch(day){
			case 1:return "星期日";
			case 2:return "星期一";
			case 3:return "星期二";
			case 4:return "星期三";
			case 5:return "星期四";
			case 6:return "星期五";
			case 7:return "星期六";
			default:return "";
		}
	}
	
	/**
	 * 获取天气
	 */
	public String getWeatherName(int catalog){
		switch(catalog){
			case 1:return "晴";
			case 2:return "阴";
			case 3:return "雨";
			case 4:return "雪";
			case 5:return "沙尘";
			default:return "晴";
		}
	}
}
